package com.example.demo.aa;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static void main(String[] args) {
        main2();
    }

    public static void main2() {
        String path = "D:\\桌面\\10\\abc.xlsx";

        List<Bean2> bean2ListSum = new ArrayList<>();
        for (int i = 1; i < 40; i++) {
            //通过sheet名获取
            ExcelReader reader = ExcelUtil.getReader(FileUtil.file(path), "Sheet" + i);
            List<Bean2> list = reader.readAll(Bean2.class);
            bean2ListSum.addAll(list);
        }

        List<Bean2> bean2ListSumResult = new ArrayList<>();
        List<Bean2> bean2ListSumResultPaixu = new ArrayList<>();
        formatList(bean2ListSum, bean2ListSumResult);

        paixuList(bean2ListSumResult, bean2ListSumResultPaixu);

        excelWriterBean3(bean2ListSumResultPaixu);
    }

    public static void formatList(List<Bean2> rows1,
                                  List<Bean2> bean2ListSumResult) {


        List<Bean2> bean2List1 = new ArrayList<>();
        int i = 1;
        for (int j = 0; j < rows1.size(); j += 2) {
            Bean2 bean2 = rows1.get(j);
            if (bean2.get交易单号() == null || "".equals(bean2.get交易单号())) {
                break;
            }
            Bean2 bean_rows1 = rows1.get(i);
            Bean2 bean2_1 = new Bean2();
            bean2_1.set交易单号(bean2.get交易单号() + bean_rows1.get交易单号());
            bean2_1.set交易时间(bean2.get交易时间() + " " + bean_rows1.get交易时间());
            bean2_1.set交易类型(bean2.get交易类型() + bean_rows1.get交易类型());
            bean2_1.set收支(bean2.get收支());
            bean2_1.set交易方式(bean2.get交易方式());
            bean2_1.set金额(bean2.get金额());
            bean2_1.set交易对方(bean2.get交易对方() + bean_rows1.get交易对方());
            bean2_1.set商户单号(bean2.get商户单号() + bean_rows1.get商户单号());
            i += 2;
            bean2List1.add(bean2_1);
        }

        bean2ListSumResult.addAll(bean2List1);

    }

    public static void paixuList(List<Bean2> bean2ListSumResult,
                                 List<Bean2> bean2ListSumResultPaixu) {
        for (int i = bean2ListSumResult.size()-1; i >= 0; i--) {
            Bean2 bean2 = bean2ListSumResult.get(i);
            bean2ListSumResultPaixu.add(bean2);
        }
    }

    public static void excelWriterBean3(List<Bean2> rows) {

        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("d:/Bean2-2.xlsx");
        // 合并单元格后的标题行，使用默认标题样式
        // writer.merge(4, "一班成绩单");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
        // 关闭writer，释放内存
        writer.close();
    }

}
